/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.moedik.webservice;

import com.moedik.classes.BusService;
import com.moedik.classes.MailSender;
import com.moedik.classes.ShippingService;
import com.moedik.classes.ShuttleService;
import com.moedik.model.BusModel;
import com.moedik.model.ConfigModel;
import com.moedik.model.FlightModel;
import com.moedik.model.PaymentModel;
import com.moedik.model.ShippingModel;
import com.moedik.model.ShuttleModel;
import com.moedik.model.UserModel;
import java.util.ArrayList;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

/**
 *
 * @author Sammy
 */
@WebService(serviceName = "MoedikWS")
public class MoedikWS {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getCompanyName")
    public String getCompanyName() {
        //TODO write your implementation code here:        
        ConfigModel configModel = new ConfigModel();
        String name = configModel.getCompanyName();

        return name;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "registerPackageShipment")
    public int registerPackageShipment(@WebParam(name = "username") String username, @WebParam(name = "password") String password, @WebParam(name = "serviceId") int serviceId, @WebParam(name = "weight") int weight, @WebParam(name = "recipient") String recipient, @WebParam(name = "phone") String phone) {
        UserModel userModel = new UserModel();
        ShippingModel shippingModel = new ShippingModel();

        int userId = userModel.validate(username, password);

        if (userId > 0) {
            int packageId = shippingModel.register(userId, serviceId, weight, recipient, phone);

            return packageId;
        } else {
            return -1;
        }
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getShippingServices")
    public ArrayList<ShippingService> getShippingServices() {
        ArrayList<ShippingService> shippingServices = null;

        ShippingModel shippingModel = new ShippingModel();
        shippingServices = shippingModel.getServices();

        return shippingServices;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getBusServices")
    public ArrayList<BusService> getBusServices() {
        ArrayList<BusService> busServices = null;

        BusModel busModel = new BusModel();
        busServices = busModel.getServices();

        return busServices;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getShuttleServices")
    public ArrayList<ShuttleService> getShuttleServices() {
        ArrayList<ShuttleService> shuttleServices = null;

        ShuttleModel shuttleModel = new ShuttleModel();
        shuttleServices = shuttleModel.getServices();

        return shuttleServices;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "bookShuttle")
    public String bookShuttle(@WebParam(name = "username") String username, @WebParam(name = "password") String password, @WebParam(name = "serviceId") int serviceId, @WebParam(name = "date") long date) {
        //TODO write your implementation code here:
        String billno = null;

        UserModel userModel = new UserModel();
        ShuttleModel shuttleModel = new ShuttleModel();

        int userId = userModel.validate(username, password);

        if (userId > 0) {
            if (shuttleModel.available(serviceId, date)) {
                billno = shuttleModel.book(userId, serviceId, date);
            }
        }

        return billno;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "bookBus")
    public String bookBus(@WebParam(name = "username") String username, @WebParam(name = "password") String password, @WebParam(name = "serviceId") int serviceId, @WebParam(name = "date") long date) {
        //TODO write your implementation code here:
        String billno = null;

        UserModel userModel = new UserModel();
        BusModel busModel = new BusModel();

        int userId = userModel.validate(username, password);

        if (userId > 0) {
            if (busModel.available(serviceId, date)) {
                billno = busModel.book(userId, serviceId, date);
            }
        }

        return billno;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "bookFlight")
    public String bookFlight(@WebParam(name = "username") String username, @WebParam(name = "password") String password, @WebParam(name = "serviceId") int serviceId, @WebParam(name = "date") long date) {
        //TODO write your implementation code here:
        String billno = null;

        UserModel userModel = new UserModel();
        FlightModel flightModel = new FlightModel();

        int userId = userModel.validate(username, password);

        if (userId > 0) {
            //if (flightModel.available(serviceId, date)) {
            billno = flightModel.book(userId, serviceId, String.valueOf(date));
            //}
        }

        return billno;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "confirmPayment")
    public String confirmPayment(@WebParam(name = "username") String username, @WebParam(name = "password") String password, @WebParam(name = "billno") String billno) {
        //TODO write your implementation code here:
        String result = null;

        UserModel userModel = new UserModel();
        PaymentModel paymentModel = new PaymentModel();

        int userId = userModel.validate(username, password);

        if (userId > 0) {
            result = paymentModel.confirm(billno);
        }

        return result;
    }

}
